<template>
    <div>
        <el-dialog :visible.sync="value" center :show-close="false" width="10%">
            <el-upload
                    ref="upload"
                    style="float: right;margin-bottom: 10px"
                    name="file"
                    :limit="1"
                    :show-file-list="false"
                    :data="{examId:parentData.id}"
                    :headers="{'token': token()}"
                    action="http://47.97.77.138:56700/student/exam/upload"
                    :on-success="uploadSuccess"
                    :on-exceed="uploadExceed"
                    :on-change="uploadChange"
                    :on-error="uploadError"
                    :file-list="fileList">
                <el-button size="mini" type="danger" plain icon="el-icon-upload">上传</el-button>
            </el-upload>
        </el-dialog>
    </div>
</template>

<script>

    import { getToken } from '@/utils/tool';

    export default {
        name: "student-exam-upload",
        data(){
            return {
                value:false,
                parentData:{},
                fileList:[]
            }
        },
        methods:{
            token(){
                return getToken();
            },
            uploadSuccess(response,file,filelist){ //response中有该上机题的id 通过id进行表单数据的修改
                this.$message.success("上传成功");
                this.$parent.getList(this.$parent.params);
                this.value = false;
            },
            uploadError(err,file,fileList){
                this.$message.error(file.name + "文件上传失败");
                this.$refs.upload.uploadFiles = [];
                return false;
            },
            uploadExceed(){
                this.$message.error("上传的文件超出了限定了个数");
                this.$refs.upload.uploadFiles = [];
                return false;
            },
            uploadChange(file,fileList){

                if( !(file.raw.type === 'application/x-zip-compressed')){
                    this.$refs.upload.uploadFiles = [];
                    this.$message.error("只能上传ZIP文件");
                    return false;
                }
                if(file.size / 1024 / 1024 > 200){
                    this.$refs.upload.uploadFiles = [];
                    this.$message.error("上传的文件不能超过200MB");
                    return false;
                }
            },
        }
    }
</script>

<style scoped>

</style>